﻿using DataAccessObjectLayer;
using ModelLayer;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace ControllerLayer
{
    public class StationCtr : IStationCtr
    {
        /// <summary>
        /// Adds the battery.
        /// </summary>
        /// <param name="stationId">The station identifier.</param>
        public void AddBattery(long stationId)
        {
            DBStation dbS = new DBStation();
            var station = dbS.GetStation(stationId);
            station.Batteries += 1;
            dbS.UpdateStation(station);
        }

        /// <summary>
        /// Removes the battery.
        /// </summary>
        /// <param name="id">The identifier.</param>
        public bool RemoveBattery(long id)
        {
            DBStation dbS = new DBStation();
            var station = dbS.GetStation(id);
            bool result = false;
            if (station.Batteries > 0)
            {
                station.Batteries -= 1;
                result = true;
                dbS.UpdateStation(station);
            }
            return result;
        }

        /// <summary>
        /// Remove batteries from listed stations, return false it not posible.
        /// </summary>
        /// <param name="stationList">StationList where batteries are to be removed from</param>
        public bool RemoveBatteries(List<Station> stationList)
        {
            IDBStation dbStation = new DBStation();
            return dbStation.RemoveBatteries(stationList);
        }

        public int GetBatteries(long id)
        {
            return GetStation(id).Batteries;
        }

        public void SetBatteries(long id, int number)
        {
            DBStation dbS = new DBStation();
            var station = dbS.GetStation(id);
            station.Batteries = number;
            dbS.UpdateStation(station);
        }

        /// <summary>
        /// Creates a station in the database.
        /// </summary>
        /// <param name="station">The station.</param>
        /// <returns>The number of rows affected</returns>
        public int CreateStation(Station station)
        {
            DBStation dbS = new DBStation();
            return dbS.InsertStation(station);
        }

        /// <summary>
        /// Gets the station.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns>Station.</returns>
        public Station GetStation(long id)
        {
            DBStation dbS = new DBStation();
            return dbS.GetStation(id);
        }

        /// <summary>
        /// Updates a station in the database.
        /// </summary>
        /// <param name="station">The station.</param>
        /// <returns>The number of rows affected</returns>
        public int UpdateStation(Station station)
        {
            DBStation dbS = new DBStation();
            return dbS.UpdateStation(station);
        }

        /// <summary>
        /// Deletes a station in the database.
        /// </summary>
        /// <param name="stationId">The station id.</param>
        /// <returns>The number of rows affected</returns>
        public int DeleteStation(long stationId)
        {
            try
            {
                DBStation dbS = new DBStation();
                return dbS.RemoveStation(stationId);
            }
            catch (SqlException)
            {
                return -1;
            }
        }

        /// <summary>
        /// Gets all stations in the database.
        /// </summary>
        /// <returns>List{Station}.</returns>
        public List<Station> GetAllStation()
        {
            DBStation dbS = new DBStation();
            return dbS.GetAllStations();
        }

        /// <summary>
        /// Gets all edges in the database.
        /// </summary>
        /// <returns>List{Edge}.</returns>
        public List<Edge> GetAllEdges()
        {
            DBEdge dbE = new DBEdge();
            return dbE.GetAllEdges();
        }

        /// <summary>
        /// Gets edges from a station in the database.
        /// </summary>
        /// <param name="stationId">The station identifier.</param>
        /// <returns>List{Edge}.</returns>
        public List<Edge> GetStationEdges(long stationId)
        {
            DBEdge dbE = new DBEdge();
            return dbE.GetEdges(stationId);
        }

        /// <summary>
        /// Creates an edge in the database.
        /// </summary>
        /// <param name="edge">The edge.</param>
        /// <returns>The number of rows affected</returns>
        public int CreateEdge(Edge edge)
        {
            DBEdge dbE = new DBEdge();
            return dbE.CreateEdge(edge);
        }

        /// <summary>
        /// Deletes an edge in the database between two stations.
        /// </summary>
        /// <param name="station1">The station1.</param>
        /// <param name="station2">The station2.</param>
        /// <returns>The number of rows affected</returns>
        public int DeleteEdge(long station1, long station2)
        {
            DBEdge dbE = new DBEdge();
            return dbE.DeleteEdge(station1, station2);
        }
    }
}